//
//  ZYCOnlinePreviewViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/10/12.
//

#import "ZYCOnlinePreviewViewController.h"
#import <WebKit/WebKit.h>
@interface ZYCOnlinePreviewViewController ()<WKNavigationDelegate>

@property (nonatomic, strong) WKWebView *webView;
@property (strong, nonatomic) NSURL *filePath;
@property (strong, nonatomic) UIView *navi;
@end

@implementation ZYCOnlinePreviewViewController

- (instancetype)initWithFilePath:(NSURL *)filePath
{
    if (self = [super init]) {
        self.filePath = filePath;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight)];
    self.navi.backgroundColor = [UIColor whiteColor];
    UIButton *back = [[UIButton alloc] initWithFrame:CGRectMake(16, kNavBarAndStatusBarHeight-44, 50, 44)];
    [back setImage:[UIImage imageNamed:@"close2"] forState:UIControlStateNormal];
    [back setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 35)];
    [back addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.navi addSubview:back];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth-200)/2, safeAreaTopHeight-44, 200, 44)];
    title.font = [UIFont boldSystemFontOfSize:17];
    title.text = @"查看附件";
    title.textAlignment = NSTextAlignmentCenter;
    [self.navi addSubview:title];
    [self.view addSubview:self.navi];

    [self.view addSubview:self.webView];
    [self.webView loadRequest:[NSURLRequest requestWithURL:self.filePath]];
}

-(WKWebView *) webView{
    if (!_webView) {
        NSString *jScript =
        @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport');\
        meta.setAttribute('content', 'width=device-width');\
        document.getElementsByTagName('head')[0].appendChild(meta); \
        var imgs = document.getElementsByTagName('img');\
        for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';};";
       WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
       WKUserContentController *wkUController = [[WKUserContentController alloc] init];
       [wkUController addUserScript:wkUScript];
       WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init];
        webConfig.userContentController = wkUController;
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight) configuration:webConfig];
        _webView.navigationDelegate = self;
        _webView.scrollView.showsVerticalScrollIndicator = NO;
        _webView.scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _webView;
}


- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
}

- (void)back:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - WKNavigationDelegate

#pragma mark - WKNavigationDelegate

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
    NSLog(@"webViewDidStartLoad");
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"webViewDidFinishLoad");
    //禁用用户选择 禁止选中
    [webView evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none';" completionHandler:nil];
    //禁用长按弹出框 禁用系统弹框(长链接,表格 图片之类的)
    [webView evaluateJavaScript:@"document.documentElement.style.webkitTouchCallout='none';" completionHandler:nil];
    
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{
    NSLog(@"webViewDidFail:%@",error);
//    if (error.code == - 1001) {
//        self.noDataView.hidden = NO;
//    }

}

///* 页面加载完成 */
//- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
//    NSString *injectionJSString = @"var script = document.createElement('meta');"
//    "script.name = 'viewport';"
//    "script.content=\"width=device-width, user-scalable=no\";"
//    "document.getElementsByTagName('head')[0].appendChild(script);";
//    [webView evaluateJavaScript:injectionJSString completionHandler:nil];
//
//}



//WKWebview的使用后加载h5页面后不能正常点击里面的链接
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    //如果是跳转一个新页面
    if (navigationAction.targetFrame == nil) {
        [webView loadRequest:navigationAction.request];
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
